--local scheduler=require("cocos.framework.scheduler")
local sound_path_base="sound"
local ui_sound_path_base="sound/UI"
local yy_sound_path_base="sound/MH"
local story_sound_path_base="sound/TC"
local guide_sound_path_base="sound/YD"
local guild_tech_sound_path_base="sound/Association_tech_skill"
local sound_work="sound_work"
local music_work="music_work"

local FzSound={
    sounds={
        touch_name=sound_path_base.."/UI/UI_dianji.mp3",
    },
    music={
        main_name=sound_path_base.."/UI/UI_main.mp3",
        battle_name=sound_path_base.."/UI/UI_battle.mp3",
    },
    _timer=nil,
    _current_music==nil,
}

function FzSound.init()
    local audio=audio
    for key, name in pairs(FzSound.music) do
        audio.preloadMusic(name)
    end
    for key, name in pairs(FzSound.sounds) do
    	audio.preloadSound(name)
    end
    local sound=cc.UserDefault:getInstance():getBoolForKey(sound_work,true)
    local music=cc.UserDefault:getInstance():getBoolForKey(music_work,true)
    FzSound.setSoundWork(sound)
    FzSound.setMusicWork(music)
    if music then 
        FzSound.ChangeMusic(FzSound.music.main_name)
    end
end

function FzSound.setSoundWork(work)
	if work then
		audio.setSoundsVolume(1)
	else
	   audio.setSoundsVolume(0)
	end
	cc.UserDefault:getInstance():setBoolForKey(sound_work,work)
end

function FzSound.getSoundWork()
    local sound=cc.UserDefault:getInstance():getBoolForKey(sound_work,true)
    return sound
end

function FzSound.getMusicWork()
    local music=cc.UserDefault:getInstance():getBoolForKey(music_work,true)
    return music
end

function FzSound.setMusicWork(work)
	if work then
		audio.setMusicVolume(1)
	else
	   audio.setMusicVolume(0)
	end
	cc.UserDefault:getInstance():setBoolForKey(music_work,work)
end

function FzSound.playGuildTechSkill( skill_id )
    local name=string.format("%s/skill_%d.mp3",guild_tech_sound_path_base,skill_id)
    audio.playSound(name,false)
end

function FzSound.playBattleSound(sound_name)
    if sound_name~="0" then
        local name = string.format("%s/%s.mp3",sound_path_base,sound_name)
        audio.playSound(name,false)
    end
end

local last_getherosound=nil
function FzSound.playGetHeroSound(heroid)
    if heroid~=0 then
        FzSound.clearLastGetHeroSound()
        local name = string.format("sound/zh/%d_zh.mp3", heroid)
        last_getherosound = audio.playSound(name,false)
    end
end

function FzSound.clearLastGetHeroSound()
    if last_getherosound then 
        audio.stopSound(last_getherosound)
        last_getherosound=nil
    end
end

function FzSound.playUISound(sound_name)
    audio.playSound(ui_sound_path_base.."/"..sound_name..".mp3",false)
end

function FzSound.playYY(yyname)
	audio.playSound(yy_sound_path_base.."/"..yyname..".mp3",false)
end

local last_story=nil
function FzSound.playStorySound(sound_name)
	FzSound.clearLastStory()
	last_story=audio.playSound(story_sound_path_base.."/"..sound_name..".mp3",false)
end

function FzSound.clearLastStory()
    if last_story then
        audio.stopSound(last_story)
        last_story=nil
    end
end

local last_guide=nil
function FzSound.playGuideSound(sound_name)
    FzSound.clearLastGuide()
    last_guide=audio.playSound(guide_sound_path_base.."/"..sound_name..".mp3",false)
end

function FzSound.clearLastGuide()
    if last_guide then
        audio.stopSound(last_guide)
        last_guide=nil
    end
end

function FzSound.delayChangeMusic(music,delay)
    if FzSound._current_music ~= music then 
        FzSound._current_music=music
        if FzSound.getMusicWork() then
            FzSound.clearTimer()
            local total_time=delay
            local time=delay
            FzSound._timer=scheduler.scheduleUpdateGlobal(function(delta)
                time=time-delta
                if time>0 then
                    audio.setMusicVolume(time/total_time)
                else
                    audio.setMusicVolume(1)
                    audio.playMusic(music,true)
                    FzSound.clearTimer()
                end
            end)
        end
    end
end

function FzSound.ChangeMusic(music_name)
    if FzSound._current_music ~= music_name then 
        FzSound._current_music=music_name
        if FzSound.getMusicWork() then 
            audio.playMusic(music_name,true)
        end
    end
end

function FzSound.clearTimer()
	if FzSound._timer then
		scheduler.unscheduleGlobal(FzSound._timer)
		FzSound._timer=nil
	end
end

return FzSound